﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MainFrmTool.ToolBox;
using MainFrameService;
using MainFrameDesign;
using MainFrameDesign.Action;
using MainFrameDesign.ContentContorl;
using System.Xml;
using MainZNQD;
using MainService;
using MainBuilder;
using MainInterface;
using MainFile;
using MainFrmTool.CustomTab;
namespace MainFrame
{
    public partial class MainFrm : Form
    {
        NameService nameservice = new NameService();
        public MainFrm()
        {
            InitializeComponent();
            MyInit();
        }

        private void MyInit()
        {
            MyTab.TabPages.Clear();
            MainTool.OnDrag += MainToolBox_OnDrag;
            MainGrid.PropertyTabs.AddTabType(typeof(CustomEventTab), PropertyTabScope.Document);
        }

        DragObj CurrentAction = null;
        private void MainToolBox_OnDrag(ToolBoxItem ai)
        {
            if (MyTab.TabPages.Count == 0) 
            {
                return;
            }
            PageObj pageobj = MyTab.TabPages[MyTab.SelectedIndex].Tag as PageObj;
            DesignControl MyPan = pageobj.Design;
            CurrentAction = new DragObj(MyPan, MyPan.Root);
            CurrentAction.CurrentDiv = ai.CreateNew(nameservice.GetName(ai.TypeName));
            CurrentAction.TypeName = ai.TypeName;
            CurrentAction.CurrentDiv.IsVitrul = true;
            CurrentAction.Root = MyPan.Root;
            MainTool.DoDragDrop(CurrentAction, DragDropEffects.Copy);
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            
        }

        private void LoadToolMB()
        {
            if (MainTool.Groups.Count > 0) 
            {
                return;
            }
            MainFrmTool.ToolBox.ToolBoxGroup group = new MainFrmTool.ToolBox.ToolBoxGroup();
            group.Text = "布局组件";
            MainTool.Groups.Add(group.Text,group);
            MainFrmTool.ToolBox.ToolBoxItem item = new MainFrmTool.ToolBox.ToolBoxItem();
            item.Text = "普通容器";
            item.TypeName = "Div";
            group.AppendItem(item);
            item = new MainFrmTool.ToolBox.ToolBoxItem();
            item.Text = "左右切分容器";
            item.TypeName = "SplitLR";
            group.AppendItem(item);
            group.IsOpen = true;
        }

        private IBuilder CreateOneDesing(string tabname, string description)
        {
            MyTab.TabPages.Insert(0, tabname);
            MyTab.SelectedIndex = 0;
            MyTab.ShowToolTips = true;
            MyTab.TabPages[0].ToolTipText = description;
            TabPage page = MyTab.TabPages[0];
            DesignControl design = new DesignControl();
            design.Name = nameservice.GetName("DesignControl");
            design.Dock = DockStyle.Fill;
            page.Controls.Add(design);
            PageObj pageobj = new PageObj();
            pageobj.Design = design;
            pageobj.NameService = new NameService();
            page.Tag = pageobj;
            design.DesignDragDrop += design_DesignDragDrop;
            design.DesignDragEnter += design_DesignDragEnter;
            design.DesignDragLeave += design_DesignDragLeave;
            design.DesignDragOver += design_DesignDragOver;
            design.DrawPanel.MouseClick += DrawPanel_MouseClick;
            return design.Root;
        }

        void DrawPanel_MouseClick(object sender, MouseEventArgs e)
        {
            PageObj pageobj = MyTab.TabPages[MyTab.SelectedIndex].Tag as PageObj;
            DesignControl MyPan = pageobj.Design;
            Point P = e.Location;
            P.Y += MyPan.GetScrollValue();
            Div div = MyPan.Root.IsSelect(P);
            SetSelect(div);
        }

        void design_DesignDragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
        Point MoPoint;
        void design_DesignDragOver(object sender, DragEventArgs e)
        {
            PageObj pageobj = MyTab.TabPages[MyTab.SelectedIndex].Tag as PageObj;
            DesignControl MyPan = pageobj.Design;
            Control control = (Control)sender;
            CurrentAction.CurrentPoint.X = e.X;
            CurrentAction.CurrentPoint.Y = e.Y;
            CurrentAction.CurrentPoint = control.PointToClient(CurrentAction.CurrentPoint);
            CurrentAction.CurrentPoint.Y += MyPan.GetScrollValue();
            MoPoint = CurrentAction.CurrentPoint;
            CurrentAction.Hover();
        }

        void design_DesignDragLeave(object sender, EventArgs e)
        {
            PageObj pageobj = MyTab.TabPages[MyTab.SelectedIndex].Tag as PageObj;
            DesignControl MyPan = pageobj.Design;
            CurrentAction.CurrentDiv.RemoveSelf(true);
            CurrentAction.Leave();
            MyPan.InvalidateDesign();
        }
        void design_DesignDragDrop(object sender, DragEventArgs e)
        {
            PageObj pageobj = MyTab.TabPages[MyTab.SelectedIndex].Tag as PageObj;
            DesignControl MyPan = pageobj.Design;
            CurrentAction.CurrentDiv.Name =pageobj.NameService.GetName(CurrentAction.TypeName);
            CurrentAction.CurrentDiv.IsVitrul = false;
            MyPan.InvalidateDesign();
            SetSelect(CurrentAction.CurrentDiv);
        }
        public void SetSelect(object obj) 
        {
            if (MainGrid.Tag !=null && MainGrid.Tag is Div) 
            {
                Div div = MainGrid.Tag as Div;
                div.IsSelected = false;
            }
            MainGrid.Tag = obj;
            if (obj == null || !(obj is Div)) 
            {
                MainGrid.SelectedObject = obj;
            }
            else
            {
                MainGrid.Tag = obj;
                Div div = obj as Div;
                MainGrid.SelectedObject = div.PMC;
                div.IsSelected = true;
            }

            PageObj pageobj = MyTab.TabPages[MyTab.SelectedIndex].Tag as PageObj;
            if (pageobj != null)
            {
                DesignControl MyPan = pageobj.Design;
                MyPan.InvalidateDesign();
            }
            
        }

        private void MainGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            PageObj pageobj = MyTab.TabPages[MyTab.SelectedIndex].Tag as PageObj;
            DesignControl MyPan = pageobj.Design;
            if (e.ChangedItem.Label != "Name")
            {
                MyPan.Root.CallDrawPar();
            }
            MyPan.InvalidateDesign();
        }

        private void MyTab_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetSelect(null);
        }
        LoadFrm loadfrm = null;
        Solution solution = new Solution();
        private void 打开解决方案ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            loadfrm = new LoadFrm();
            if (loadfrm.ShowDialog() == DialogResult.OK) 
            {
                XmlDocument doc = loadfrm.docznqd;
                solution.LoadXml(doc);
                MyServer.OldCurrentSolution = solution;
                foreach (var item in solution.Activities)
                {
                    item.Buider=CreateOneDesing(item.Name,item.Description);
                }
                LoadToolMB();
                doc = loadfrm.doczyk;
                this.MainTool.LoadXml(doc);
                this.MainTool.InvalidateDraw();
            }
        }

        private void 生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MyServer.OldCurrentSolution == null) 
            {
                return;
            }
            string file = MyServer.OldCurrentSolution.File;
            string dirpath = MyFile.DownFileZipSolution(file);
            if (!string.IsNullOrEmpty(dirpath)) 
            {
                List<string> files = new List<string>();
                foreach (var item in MyServer.OldCurrentSolution.Activities)
                {
                    IBuilder buider = item.Buider;
                    buider.GetFiles(files);
                    string dom = buider.GetDom(string.Empty);
                    string css = buider.GetCSS(string.Empty);
                    string reference = buider.GetReferences(string.Empty);
                    string script = buider.GetScript(string.Empty);
                    string links = buider.GetLinks(string.Empty);
                    string html = Builder.GetHtml(MyServer.OldCurrentSolution.Name,item.Name, dom, css, script, reference, links);
                    MyFile.CreateHtml(dirpath, item.Name, html);
                }
                Dictionary<string, string> dic = new Dictionary<string, string>();
                foreach (var item in files)
                {
                    if (!dic.ContainsKey(item))
                    {
                        dic.Add(item, "");
                    }
                }
                string[] strs = dic.Keys.ToArray();

                foreach (var item in strs)
                {
                    MyFile.DownFileZipComponent(MyServer.OldCurrentSolution.Name,item);
                }

                if (MessageBox.Show("程序已经生成完成，是否部署?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string todir="C:\\AppServ\\www\\"+MyServer.OldCurrentSolution.Name;
                    MyFile.DeleteDir(todir);
                    MyFile.CopyDirectory(dirpath, todir);
                    System.Diagnostics.Process.Start("iexplore.exe", "http://localhost:9999/"+MyServer.OldCurrentSolution.Name+"/");  
                }
            }

        }

        private void MainGrid_TabIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(MainGrid.TabIndex.ToString());
        }
    }
}
